package com.longxiao.validator.parser;


import com.longxiao.validator.ValidResult;
import com.longxiao.validator.anno.ValiNotBlank;

import java.lang.annotation.Annotation;

/**
 * Created by houhongwei on 2017/5/12.
 */
public class ValiNotBlankParser extends Parser {

    @Override
    protected ValidResult doValid(Object value, Annotation annotation) {
        ValiNotBlank anno = (ValiNotBlank) annotation;
        if (value == null) {
            return ValidResult.createFail(anno.err());
        }
        if (value.getClass().isArray()) {
            String s = (((String[]) value)[0]);
            if ("".equals(s)) {
                return ValidResult.createFail(anno.err());
            }
        }
        if (value instanceof String) {
            String s = (String) value;
            if ("".equals(s)) {
                return ValidResult.createFail(anno.err());
            }
        }
        return ValidResult.createSucc();
    }

    @Override
    public Class getAnno() {
        return ValiNotBlank.class;
    }

    @Override
    protected boolean isRequired(Annotation annotation) {
        return Boolean.TRUE;
    }

    @Override
    protected String getErr(Annotation annotation) {
        return ((ValiNotBlank) annotation).err();
    }
}
